package my.interview;

import java.util.*;

/**
 * @DESCRIPTION:
 * @USER: shg
 * @DATE: 2022/12/28 12:30
 */
public class L_34 {
    public static void main(String[] args) {
        int nums[] = new int[]{5, 7, 7, 8, 8, 10};
        int target = 8;
        int[] ints = searchRange(nums, target);
        System.out.println(Arrays.toString(ints));

        String str = "aaa";
        int length = str.length();
    }

    public static int[] searchRange(int[] nums, int target) {
        if (nums.length == 0) {
            return new int[]{-1, -1};
        }

        TreeMap<Integer, Integer> map = new TreeMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(i, nums[i]);
        }
        Set<Integer> set = map.keySet();
        Set<Integer> result = new HashSet<>();
        for (Integer key : set) {
            if (map.get(key) == target) {
                result.add(key);
            }
        }

        if (result.size() == 0) {
            return new int[]{-1, -1};
        }

        Integer[] res = result.toArray(new Integer[0]);
        int[] res1 = new int[res.length];
        for (int i = 0; i < res.length; i++) {
            res1[i] = res[i];
        }

        if (res1.length == 1) {
            return new int[]{res1[0], res1[0]};
        }
        if (res1.length == 2) {
            return res1;
        }

        Arrays.sort(res1);
        return new int[]{res1[0], res1[res1.length - 1]};
    }
}
